// Copyright (C) 2025, ZWSOFT Co., LTD.(Guangzhou)
// Licensed under the MIT License. See LICENSE in the project root for license information.

using CommunityToolkit.Mvvm.Input;
using System.Text.Json;
using TestClient.Domain;

namespace TestClient.ViewModel;
public partial class MBDTestViewModel : PageViewModelBase
{
    public IAsyncRelayCommand CreateMBDCommand { get; }

    public MBDTestViewModel(VFAdapterWrapper wrapper, ISnackNotifyService notifyService) : base(wrapper, notifyService)
    {
        CreateMBDCommand = CreateAsyncRpcCommand(CreateMBDAsync);
    }

    private async Task CreateMBDAsync()
    {
        EnsureConnected();
        await Task.Run(() =>
        {
            var options = new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            };

            var mbdroot1 = JsonSerializer.Deserialize<MBDTreeNode>(Root1, options) ?? throw new Exception("Root1 JSON parse failed");
            var mbdroot2 = JsonSerializer.Deserialize<MBDTreeNode>(Root2, options) ?? throw new Exception("Root2 JSON parse failed");
            Wrapper.CreateMBDTree(mbdroot1, mbdroot2);
        });
    }

    const string Root1 = @"
{
    ""childNodes"": [
        {
            ""childNodes"": [
                {
                    ""childNodes"": [],
                    ""key"": ""ply001"",
                    ""nodeName"": ""FibPly"",
                    ""value"": """",
                    ""valueType"": 2
                },
                {
                    ""childNodes"": [],
                    ""key"": ""ply002"",
                    ""nodeName"": ""FibPly"",
                    ""value"": """",
                    ""valueType"": 2
                }
            ],
            ""key"": ""laminate001"",
            ""nodeName"": ""FibLaminate"",
            ""value"": """",
            ""valueType"": 2
        }
    ],
    ""key"": ""CCDS Base Geometry"",
    ""nodeName"": """",
    ""value"": """",
    ""valueType"": 2
}
";
    const string Root2 = @"
{
    ""childNodes"": [
        {
            ""childNodes"": [
                {
                    ""childNodes"": [],
                    ""key"": ""Sequence"",
                    ""nodeName"": ""FibLaminate"",
                    ""value"": ""A"",
                    ""valueType"": 0
                },
                {
                    ""childNodes"": [],
                    ""key"": ""Step"",
                    ""nodeName"": ""FibLaminate"",
                    ""value"": ""10"",
                    ""valueType"": 0
                },
                {
                    ""childNodes"": [
                        {
                            ""childNodes"": [],
                            ""key"": ""Sequence"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""A"",
                            ""valueType"": 0
                        },
                        {
                            ""childNodes"": [],
                            ""key"": ""Step"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""20"",
                            ""valueType"": 0
                        },
                        {
                            ""childNodes"": [],
                            ""key"": ""Material"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""T-12-in"",
                            ""valueType"": 0
                        },
                        {
                            ""childNodes"": [],
                            ""key"": ""Specified Orientation"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""0"",
                            ""valueType"": 0
                        }
                    ],
                    ""key"": ""ply001"",
                    ""nodeName"": ""FibPly"",
                    ""value"": """",
                    ""valueType"": 2
                },
                {
                    ""childNodes"": [
                        {
                            ""childNodes"": [],
                            ""key"": ""Sequence"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""A"",
                            ""valueType"": 0
                        },
                        {
                            ""childNodes"": [],
                            ""key"": ""Step"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""30"",
                            ""valueType"": 0
                        },
                        {
                            ""childNodes"": [],
                            ""key"": ""Material"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""NCF-3_layer"",
                            ""valueType"": 0
                        },
                        {
                            ""childNodes"": [],
                            ""key"": ""Specified Orientation"",
                            ""nodeName"": ""FibPly"",
                            ""value"": ""0"",
                            ""valueType"": 0
                        }
                    ],
                    ""key"": ""ply002"",
                    ""nodeName"": ""FibPly"",
                    ""value"": """",
                    ""valueType"": 2
                }
            ],
            ""key"": ""laminate001"",
            ""nodeName"": ""FibLaminate"",
            ""value"": """",
            ""valueType"": 2
        }
    ],
    ""key"": ""MBD008"",
    ""nodeName"": """",
    ""value"": """",
    ""valueType"": 2
}
";
}
